package com.logistics.organization.controller;

import com.logistics.organization.entity.model.OrganizationSave;
import com.logistics.organization.entity.model.OrganizationPager;
import com.logistics.organization.service.OrganizationService;
import com.logistics.utils.aspect.annotation.LoggerListener;
import com.logistics.utils.constants.LoggerMethod;
import com.logistics.utils.exception.MessageException;
import com.logistics.utils.response.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/sys/organization")
public class OrganizationController {

    @Autowired
    private OrganizationService organizationService;

    /**
     * 分页查询列表
     * @param pager
     * @return
     */
    @PostMapping("pager")
    @LoggerListener(businessName = "分页查询组织", methodName = LoggerMethod.SEARCH)
    public ResponseEntity pager(@RequestBody OrganizationPager pager){
        return ResponseUtil.success(organizationService.pager(pager));
    }

    /**
     * 查询树形
     * @return
     */
    @GetMapping("listTree")
    @LoggerListener(businessName = "查询组织树形结构", methodName = LoggerMethod.SEARCH)
    public ResponseEntity listTree(){
        return ResponseUtil.success(organizationService.listTree());
    }

    /**
     * 添加
     * @param edit
     * @return
     */
    @PutMapping("save")
    @LoggerListener(businessName = "保存组织", methodName = LoggerMethod.SAVE)
    public ResponseEntity save(@RequestBody OrganizationSave edit){
        organizationService.save(edit);
        return ResponseUtil.success();
    }

    /**
     * 查找详细
     * @param id
     * @return
     * @throws MessageException
     */
    @GetMapping("details/{id}")
    @LoggerListener(businessName = "查找组织详细", methodName = LoggerMethod.SEARCH)
    public ResponseEntity details(@PathVariable("id") String id) throws MessageException {
        return ResponseUtil.success(organizationService.detail(id));
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @DeleteMapping("remove/{ids}")
    @LoggerListener(businessName = "删除组织", methodName = LoggerMethod.DELETE)
    public ResponseEntity remove(@PathVariable String ids) throws MessageException {
        organizationService.delete(ids);
        return ResponseUtil.success();
    }
}
